﻿#region The MIT License
// The MIT License
// 
// Copyright (c) 2011 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Net.Mail;

using NUnit.Framework;

using octalforty.Staytuned.Impl.ComponentModel;
using octalforty.Staytuned.Impl.ComponentModel.Email;
//using octalforty.Staytuned.Impl.ComponentModel.Webhook;
using octalforty.Staytuned.ObjectModel;

namespace octalforty.Staytuned.Tests.Impl.ComponentModel
{
    [TestFixture]
    public class EmailNotificationSupportModuleTestFixture
    {
        /*[Test]
        [ExpectedException(typeof(ArgumentException))]
        public void PublishMessageThrowsArgumentExceptionOnUnsupportedMessageType()
        {
            var messagePublisher = new EmailNotificationSupportModule(false);
            messagePublisher.PublishMessage(new WebhookMessage(null, "text/xml", "foo"), new Uri("email:dilbert@example.com"));
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void PublishMessageThrowsFormatExceptionOnIllformedMailAddress()
        {
            var messagePublisher = new EmailNotificationSupportModule(false);
            messagePublisher.PublishMessage(new EmailMessage(null, "Sample Notification For Dilbert", "Hey, you!"), new Uri("email:dilbertexample.com"));
        }*/

        [Test]
        public void PublishMessage()
        {
            /*var notification = {}
            var messagePublisher = new EmailNotificationSupportModule(true);
            var message = messagePublisher.FormatMessage();
            messagePublisher.PublishMessage(message, new Uri("email:anton.gogolev@gmail.com"));*/
        }

        [Test]
        public void FormatMessage()
        {
            var notification = new Notification(null, new SenderDescriptor("Sender", null)) {
                ReplyTo = Address.Email("reply@example.com")
            };

            var messageFormatter = new EmailNotificationSupportModule(true) {
                TemplateRenderer = new NullTemplateRendererModule()
            };

            messageFormatter.PublishingMessage += (sender, args) => {
                var emailMessage = (EmailMessage)args.Message;
                var mailMessage = (MailMessage)args.WireMessage;

                Assert.AreEqual(Address.Email("reply@example.com"), emailMessage.ReplyTo);
                Assert.AreEqual("support@supportyapp.com", mailMessage.From.Address);
                Assert.AreEqual("Sender", mailMessage.From.DisplayName);
            };

            var message = messageFormatter.FormatMessage(
                new Template("", ""), 
                notification, 
                "event", 
                new RecipientDescriptor(Address.Email("anton.gogolev@gmail.com"), ""));
            messageFormatter.PublishMessage(message, Address.Email("anton.gogolev@gmail.com"));
        }

        [Test]
        public void FormatMessageProducesValidSubject()
        {
            var messageFormatter = new EmailNotificationSupportModule(true) {
                TemplateRenderer = new NullTemplateRendererModule()
            };

            var message = (EmailMessage)messageFormatter.FormatMessage(new Template("\r\nHi\r\n there", "hello"), new Notification(), "", Address.Email("a@b.com"));

            Assert.AreEqual("Hi there", message.Subject);
        }
    }
}
